package com.example.text2.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.text2.R;
import com.example.text2.adapter.RlvAdapter2;
import com.example.text2.bean.Bean;
import com.example.text2.bean.Video;
import com.example.text2.http.Api;
import com.example.text2.moudle.Moudle;
import com.example.text2.presenter.Presenter;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public class VideoFragment extends Fragment implements com.example.text2.view.View {


    @BindView(R.id.rlv2)
    XRecyclerView mRlv2;
    private View view;
    private Unbinder unbinder;
    private int count=10;
    RlvAdapter2 mRlvAdapter2;

    public VideoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_video, container, false);
        data();
        unbinder = ButterKnife.bind(this, inflate);
        rlv();
        return inflate;
    }

    private void rlv() {
        final ArrayList<Video.V9LG4B3A0Bean> list = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRlv2.setLayoutManager(layoutManager);
        mRlvAdapter2 = new RlvAdapter2(list,getContext());
        mRlv2.setAdapter(mRlvAdapter2);
        mRlv2.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                list.clear();
                data();
                mRlv2.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                count+=10;
                data();
            }
        });
    }

    private void data() {
        Presenter presenter = new Presenter(new Moudle(), this);
        presenter.getData(count,Api.VIDEO);
    }

    @Override
    public void showSuccess(Object o, Api api) {
        Log.e("duan", "showSuccess: ======>"+o.toString());
        String s= (String) o;
        Gson gson = new Gson();
        Video video = gson.fromJson(s, Video.class);
        List<Video.V9LG4B3A0Bean> list = video.getV9LG4B3A0();
        if (list.size()>0){
           mRlvAdapter2.addData(list);
        }
    }

    @Override
    public void showError(String error) {
        Log.e("duan", "showError: ======>"+error);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
